package com.alphaking.util;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {

	/**
	 * 将时间转成制定格式的字符串
	 * 
	 * @param date 时间
	 * @return
	 */
	public static String changeToFormatTime(String date) {
      
		Long longDate = Long.valueOf(date);		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Timestamp timestamp = new Timestamp(longDate);
        date = simpleDateFormat.format(timestamp);		
		return date;
	}
	/**
	 * 计算上次时间距离当前时间的天数
	 * @return
	 */
	public static Long getTimeDifference(Timestamp timestamp){
		Calendar cl = Calendar.getInstance();
		cl.setTimeInMillis(System.currentTimeMillis());
		Calendar cl2 = Calendar.getInstance();
		cl2.setTimeInMillis(timestamp.getTime());
		Long difference = cl.getTimeInMillis()-cl2.getTimeInMillis();
		return difference/(3600*24*1000);
	}
	
	/**
	 * 获取三天之后的时间
	 * @return
	 */
	public static String getThreeDaysLaterTime(int day){
		
		Calendar cl = Calendar.getInstance();
		Date date = new Date();
		cl.setTime(date);
		cl.set(Calendar.DATE, cl.get(Calendar.DATE) + day);
		//格式化时间
		String formatDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
				.format(cl.getTime());
		return formatDate ; 
	}
	
	public static Date getDaysLaterDate(int day){
		
		Calendar cl = Calendar.getInstance();
		Date date = new Date();
		cl.setTime(date);
		cl.set(Calendar.DATE, cl.get(Calendar.DATE) + day);
		
		return cl.getTime();
	}
	
	/**
	 * 根据拼团队伍的创建时间，获得其截止时间
	 * @param createTime
	 * @return
	 */
	public static Long getPackageTourRankEndTime(Long createTime){
		//转化为date
		Date date = new Date(createTime);
		//先获得次日的零点
		Calendar cal = Calendar.getInstance();
	    cal.setTime(new Date());
	    cal.set(Calendar.HOUR_OF_DAY, 0);
	    cal.set(Calendar.MINUTE, 0);
	    cal.set(Calendar.SECOND, 0);
	    cal.set(Calendar.MILLISECOND, 0);
	    cal.add(Calendar.DAY_OF_MONTH, 1);
	    //判断拼团队伍创建时间是在当天中午12点前还是12点后
	    if(date.getHours()>=12){
	    	//当天中午的12点后
	    	cal.add(Calendar.HOUR_OF_DAY, 12);
	    	System.out.println(cal.getTime());
	    	return cal.getTimeInMillis();
	    }else{
	    	//当天中午12点之前
	    	System.out.println(cal.getTime());
	    	return cal.getTimeInMillis();
	    }
	}
	
	/**
	 * 获取昨天凌晨的时间
	 */
	public static Date getYesterdayAmZeroDate(){
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND,0);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime();
	}
	
	/**
	 * 获取昨天中午12点的时间
	 */
	public static  Date getYesterdayPmZeroDate(){
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 12);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND,0);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		System.out.println(calendar.getTime());
		return calendar.getTime();
	}
	/**
	 * 获取一天后的时间
	 * @return
	 */
	public static Date getOneDayLater(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		System.out.println(calendar.getTime());
		return calendar.getTime();
	}
	/**
	 * 获取昨天的结束时间
	 */
	public static Date getYesterdayEndDate(){
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND,59);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime();
	}
	
	/**
	 * 获取当前时间距离指定时间的时间日期
	 * @param date
	 * @return
	 */
	public static Integer dayAfter(Timestamp time){
		
		Date now = new Date();
		Integer date = (int) ((now.getTime() - time.getTime()) 
				/ (24 * 60 * 60 * 1000));
		return date ;
	}
}
